package c.h.d.j.nio;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by hidehai on 2015/11/23.
 */
public class FileChannelDemo {

    public static  void main(String[] args) throws IOException {
        RandomAccessFile accessFile  = new RandomAccessFile("data.txt","rw");
        FileChannel fileChannel = accessFile.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(48);
        int byteRead = fileChannel.read(byteBuffer);

        while(byteRead != -1){
            System.out.println(String.format("Read: %s",byteRead));
            byteBuffer.flip();

            while(byteBuffer.hasRemaining()){
                System.out.println(byteBuffer.get());
            }

            byteBuffer.clear();
            byteRead = fileChannel.read(byteBuffer);

        }

        accessFile.close();
    }

}
